
package uestc.bitman.edxapp.downloading;

import uestc.bitman.edxapp.EdxAppConfigurations;
import uestc.bitman.edxapp.downloaded.SetVideo;
import uestc.bitman.edxapp.tools.VideoDownloadTools;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;
import java.util.List;

public class DownloadingTaskAdapter extends BaseAdapter {

    private List<DownloadingListItem> mDownloadingList;

    private Context mContext;

    @SuppressLint("HandlerLeak")
    private Handler mFinishHandler = new Handler() {
        public void handleMessage(Message msg) {

            DownloadingListItem item = (DownloadingListItem) msg.obj;

            SetVideo setVideo = new SetVideo(Environment.getExternalStorageDirectory()
                    + EdxAppConfigurations.DOWNLOAD_PATH);
            String fileName = item.getFileDownloadTask().getFileName();
            setVideo.setData(item.getCourseName(), item.getVideoTitle(), fileName);
            setVideo.setXml();
            VideoDownloadTools.urlDownloadToFile(
                    item.getFileDownloadTask().getTargetUrl().replace(".mp4", ".srt"),
                    Environment.getExternalStorageDirectory() + EdxAppConfigurations.DOWNLOAD_PATH,
                    fileName.replace(".mp4", ".srt"));
            mDownloadingList.remove(item);
            notifyDataSetChanged();
        }
    };

    public DownloadingTaskAdapter(Context context) {
        mDownloadingList = new ArrayList<DownloadingListItem>();
        this.mContext = context;
    }

    public int append(String url, String fileName, String sectionName, String chapterName,
            String courseName, String videoTitle) {

        DownloadTaskThread task = new DownloadTaskThread(url,
                Environment.getExternalStorageDirectory() + EdxAppConfigurations.DOWNLOAD_PATH
                        + fileName, EdxAppConfigurations.DEFAULT_DOWNLOAD_THREAD_NUM);
        DownloadingListItem item = new DownloadingListItem(mContext, task, sectionName, chapterName,
                courseName, videoTitle);
        mDownloadingList.add(item);
        item.setDownloadFinishHandler(mFinishHandler);

        item.startDownloadTask();

        return mDownloadingList.size();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        convertView = mDownloadingList.get(position).getItemView();
        return convertView;
    }

    public void remove(DownloadingListItem item) {
        item.cancelDownload();
        mDownloadingList.remove(item);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mDownloadingList.size();
    }

    @Override
    public DownloadingListItem getItem(int position) {
        return mDownloadingList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
}
